/*	kd:
	
	It's a string with justification for drawing.
	
	*/

struct RenderString {
	String						text;
	int							x_offset;
	
	void Set (String str, Font adjust_font, double offset = 0) {
		text		= str;
		x_offset	= -offset * adjust_font.StringWidth(str);
	}
}

/*	kd:
	
	Allows drawing at the same relative screen positions.
	
	*/

struct RenderScreen {
	double width;
	double height;
	
	void ToUserScreen () {
		width	= Screen.GetWidth();
		height	= Screen.GetHeight();
	}
	
	void ScaleToHeight (int desired_height) {
		height	= max(1, desired_height);
		width	= height * Screen.GetWidth() / Screen.GetHeight();
	}
	
	void ScaleToWidth (int desired_width) {
		width	= max(1, desired_width);
		height	= width * Screen.GetHeight() / Screen.GetAspectRatio();
	}
	
	double, double NormalToScaled (double x, double y) const {
		return x * width, y * height;
	}
	
	vector2 NormalToScaledVec (vector2 v) const {
		return (v.x * width, v.y * height);
	}
	
	double NormalToScaledX (double x) const {
		return x * width;
	}
	
	double NormalToScaledY (double y) const {
		return y * height;
	}
	
	static int HudScale () {
		Cvar hud_scale = Cvar.GetCvar("hud_scale", players [consoleplayer]);
		
		if(hud_scale) {
			int fixed_scale = min(hud_scale.GetInt(), 4);
			return fixed_scale <= 0 ? 2 : fixed_scale;
		}
		
		return 1;
	}
	
	static double CustomScale (Name cvar_name) {
		Cvar scale = Cvar.GetCvar(cvar_name, players [consoleplayer]);
		
		if(scale) {
			return max(0.001, scale.GetFloat());
		}
		
		return 1;
	}
}

/*	kd:
	
	Similar to RenderScreen, but you only use an aspect ratio. That means with
	normal coordinates, you'll always draw the same distance relative to the
	centre of the screen. This is useful for screen objects that should not
	change their position, no matter the player's aspect ratio. */

struct RenderBox {
	private double					aspect;
	private double					user_aspect;
	private double					user_width;
	private double					box_left;
	private double					box_top;
	private double					box_width;
	private double					box_forced_width; // WORST gzd feature
	private double					box_height;
	
	void ByAspect (
	double aspect_x = 4, double aspect_y = 3,
	double virtual_height	= 1000) const {
		let window_aspect = 1.0 * Screen.GetWidth() / Screen.GetHeight();
		let forced_aspect = Screen.GetAspectRatio();
		
		aspect		= aspect_x / max(0.0001, aspect_y) * window_aspect / forced_aspect;
		box_height	= max(1, virtual_height);
		user_width	= window_aspect * box_height;
		box_width	= aspect * box_height;
		box_left	= 0.5 * (user_width - box_width);
		box_top		= 0;
	}
	
	// kd: Sometimes, you want a box that is sized just right for shenanigans
	// like we did for the Sandman, where a visor has to cover a certain fov on
	// the screen. This does that. Be careful with this, though.
	
	// We scale to fov 90, of course, because I say so. If you do any fov-
	// related calibration - do so in fov 90.
	const hor_fov_90_tan_ver_fov_2	= 0.75;
	
	void RescaleForFov (double fov = 90) {
		box_left	+= 0.5 * box_width;
		box_width	/= tan(0.5 * fov);
		box_left	-= 0.5 * box_width;
		
		// TODO: top
		
		// this works but due to my INGENIOUS SETUP... it also rescales all the
		// other positions cuz I assumed box_height is the entire screen height.
		// lololololololol
		
		// double old_box_height = box_height;
		// box_height	*= hor_fov_90_tan_ver_fov_2 * aspect / tan(0.5 * fov);
		// box_top		 = 0.5 * (old_box_height - box_height);
	}
	
	double NormalXtoScreen (double x) const {
		return box_left + x * box_width;
	}
	
	double NormalYtoScreen (double y) const {
		return box_top + y * box_height;
	}
	
	double CompensateXscale (double x) const {
		return x / box_width * user_width;
	}
	
	double NormalWidthToScreen (double width) const {
		return width * box_width;
	}
	
	double NormalHeightToScreen (double height) const {
		return height * box_height;
	}
	
	double VirtualWidth () const {
		return user_width;
	}
	
	double VirtualHeight () const {
		return box_height;
	}
}
